/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.eb.entity.Customer;
import com.kaaction.modules.mer.entity.MerOrder;
import com.kaaction.modules.mer.entity.OrderSku;
import com.kaaction.modules.mer.service.MerOrderService;
import com.kaaction.modules.mer.service.OrderSkuService;

/**
 * 新订单Controller
 * @author zengbiao
 * @version 2017-05-22
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/merOrder")
public class MerOrderController extends BaseController {

	@Autowired
	private MerOrderService merOrderService;
	
	@Autowired
	private OrderSkuService orderSkuService;
	
	@ModelAttribute
	public MerOrder get(@RequestParam(required=false) String id) {
		MerOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = merOrderService.get(id);
		}
		if (entity == null){
			entity = new MerOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:merOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(MerOrder merOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MerOrder> page = merOrderService.findPage(new Page<MerOrder>(request, response), merOrder); 
		model.addAttribute("page", page);
		return "modules/mer/merOrderList";
	}

	@RequiresPermissions("mer:merOrder:view")
	@RequestMapping(value = "form")
	public String form(MerOrder merOrder, Model model) {
		model.addAttribute("merOrder", merOrder);
		
		Customer customer = merOrder.getCustomer();
		OrderSku orderSku = new OrderSku();
		
		Customer c = new Customer();
		c.setThirdpartyId(customer.getThirdpartyId());
		orderSku.setCustomer(c);
		
		orderSku.setType("30");
		
		MerOrder mOrder = new MerOrder();
		mOrder.setId(merOrder.getId());
		
		orderSku.setOrder(mOrder);
		List<OrderSku> orderSkuList = orderSkuService.findList(orderSku);
		model.addAttribute("orderSkuList", orderSkuList);
		
		return "modules/mer/merOrderForm";
	}

	@RequiresPermissions("mer:merOrder:edit")
	@RequestMapping(value = "save")
	public String save(MerOrder merOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, merOrder)){
			return form(merOrder, model);
		}
		merOrderService.save(merOrder);
		addMessage(redirectAttributes, "保存新订单成功");
		return "redirect:"+Global.getAdminPath()+"/mer/merOrder/?repage";
	}
	
	@RequiresPermissions("mer:merOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(MerOrder merOrder, RedirectAttributes redirectAttributes) {
		merOrderService.delete(merOrder);
		addMessage(redirectAttributes, "删除新订单成功");
		return "redirect:"+Global.getAdminPath()+"/mer/merOrder/?repage";
	}

}